home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11610 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  117 lines

  1. Path: solon.com!not-for-mail
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Re: const pointer confusion...
  5. Date: 25 Mar 1996 06:21:56 -0600
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4j6354$3ge@solutions.solon.com>
  10. References: <4j06gm$7oa@solutions.solon.com> <4j41hs$nku@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13.  
  14. The Amorphous Mass <robinson@blue.weeg.uiowa.edu> writes:
  15.  
  16. >On 23 Mar 1996, Reed R. Mangino wrote:
  17.  
  18. >> Could someone please straighten me out on this:
  19. >> 
  20. >> 1) const int *p = 10;
  21. >>     p is a constant pointer to an int, right? While p can be made to
  22. >>     point to something else, *p can never be assigned to, right?
  23.  
  24. >  No, p is a pointer to a constant integer. However, you are right that 
  25. >*p cannot change, but p can.
  26.  
  27. >> 2) int *const p;
  28. >>     p is a pointer to an integer.  *p can be assigned to, but p can
  29. >>     never be made to point to another address in memory, right?
  30.  
  31. >  This is a syntax error.
  32.  
  33. nausikaa:/home/kurt/src> cc cip.c
  34. nausikaa:/home/kurt/src> ./a.out
  35. 42
  36. nausikaa:/home/kurt/src> cat cip.c
  37. #include <stdio.h>
  38.  
  39. int main()
  40. {
  41.    int i = 42;
  42.    int * const p = &i;
  43.  
  44.    printf("%d\n", *p);
  45.    return 0;
  46. }
  47.  
  48. Should I complain to my vendor? Should I complain to the vendors of
  49. all compilers that accept this code?
  50.  
  51. >> 3) int const *p;
  52. >>     What the heck is this?  I can't find anything like this in my 
  53. >>     books, but my compiler thinks everything is hunky doory!???
  54.  
  55. >  This is a constant pointer to an integer.  *p can be changed, but p 
  56. >cannot.
  57.  
  58. nausikaa:/home/kurt/src> cc icp.c
  59. nausikaa:/home/kurt/src> ./a.out
  60. effffc4c
  61. nausikaa:/home/kurt/src> cat icp.c
  62. #include <stdio.h>
  63.  
  64. int main()
  65. {
  66.    const int i = 42;
  67.    int const *p;
  68.  
  69.    p = &i;
  70.    printf("%p\n", (void *)(++p));
  71.    return 0;
  72. }
  73.  
  74. Should I complain to the compiler vendor again? Obviously this compiler
  75. accepts changes to "p".
  76.  
  77. AFAIK, "int const *p;" tells us that "p" is a pointer to a constant int.
  78.  
  79. Kurt
  80. -- 
  81. | Kurt Watzka                             Phone : +49-89-2180-6254
  82. | watzka@stat.uni-muenchen.de
  83. | ua302aa@sunmail.lrz-muenchen.de
  84.  
  85. >  Also, there's
  86.  
  87. >  4) const int const *p;
  88.  
  89. >  Which is a constant pointer to a constant integer; neither *p nor 
  90. >p can change.
  91.  
  92. nausikaa:/home/kurt/src> cc cicp.c
  93. "cicp.c", line 6: invalid type combination
  94. cc: acomp failed for cicp.c
  95. nausikaa:/home/kurt/src> cat cicp.c
  96. #include <stdio.h>
  97.  
  98. int main()
  99. {
  100.    int i = 42;
  101.    const int const *p = &i;
  102.  
  103.    printf("%d\n", *p);
  104.    return 0;
  105. }
  106.  
  107. I think I'd better get a new compiler, if it cannot compile such a
  108. simple program.
  109.  
  110. AFAIK, "const int const *p;" _indeed is_ a syntax error.
  111.  
  112. Kurt
  113. --
  114. | Kurt Watzka                             Phone : +49-89-2180-6254
  115. | watzka@stat.uni-muenchen.de
  116. | ua302aa@sunmail.lrz-muenchen.de
  117.